package pixelwaks.ctrl.impl;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import common.ctrl.IObserver;
import common.gui.IViewable;

import pixelwaks.core.impl.Game;
import pixelwaks.core.impl.Player;

public class WeaponCtrl extends KeyAdapter implements IObserver {

	private List<IViewable> observers = new ArrayList<IViewable>();
	
	public void decreaseAmmo() {
		
		Player current = Game.getInstance().getCurrentPlayer();
		current.getCurrentWeapon().decreaseNumberOfShots();
		updateView();
	}
	
	private void setCurrentWeapon(int i) {
				
		if(i < Game.getInstance().getCurrentPlayer().getWeapons().size()) {
			Game.getInstance().getCurrentPlayer().setWeapon(i);
			updateView();
		}
	}
	
	public void updateView() {
		
		for(IViewable iv: observers)
			iv.updateView(null);
	}
	
	@Override
	public void keyPressed(KeyEvent e) {
		//Invoked when a key has been pressed.
		int key = e.getKeyCode();

		if(Game.lock.tryAcquire()) {
			
			if(key == KeyEvent.VK_F1) {
				
				setCurrentWeapon(0);
				
			} else if(key == KeyEvent.VK_F2) {
				
				setCurrentWeapon(1);

			} else if(key == KeyEvent.VK_F3) {
				
				setCurrentWeapon(2);
			}						
			Game.lock.release();
		}
	}
	
	@Override
	public void addView(IViewable iv) {
		
		observers.add(iv);
	}
}
